package com.ncu702.backend.controller;


import com.ncu702.backend.common.BaseResponse;
import com.ncu702.backend.common.ErrorCode;
import com.ncu702.backend.common.ResultUtils;
import com.ncu702.backend.exception.BusinessException;
import com.ncu702.backend.model.dto.user.UserLoginRequest;
import com.ncu702.backend.model.dto.user.UserRegisterRequest;
import com.ncu702.backend.model.entity.User;
import com.ncu702.backend.model.untils.TokeConfig;
import com.ncu702.backend.model.vo.LoginUserVO;
import com.ncu702.backend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/user")
@Slf4j
public class WelcomeController {

    @Resource
    private UserService userService;
    @Resource
    private TokeConfig tokeConfig;


    @GetMapping("/authentication")
    public BaseResponse<String> authenticationCheck(HttpServletRequest request){

        String jwtToken = request.getHeader("jwtToken");
        boolean authentication = userService.authentication(jwtToken);
        if(!authentication){
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR,"jwtToken失效");
        }
        User tokeMessage = tokeConfig.getTokeMessage(jwtToken);
        Long uid = tokeMessage.getUid();

        User user = userService.UserByUid(uid);
        String userName = user.getUserName();
        return ResultUtils.success(userName);
    }

    /**
     * 用户注册
     * @param userRegisterRequest
     * @return
     */
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
            return null;
        }
        long result = userService.userRegister(userAccount, userPassword, checkPassword);
        return ResultUtils.success(result);
    }

    /**
     * 用户登录
     *
     * @param userLoginRequest
     * @param
     * @return
     */
    @PostMapping("/login")
    public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest) {
        if (userLoginRequest == null) {
            ResultUtils.error(ErrorCode.PARAMS_ERROR,"请输入信息");
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            ResultUtils.error(ErrorCode.PARAMS_ERROR,"请输入信息");
        }
        LoginUserVO loginUserVO = userService.userLogin(userAccount, userPassword);
        return ResultUtils.success(loginUserVO);
    }

}
